home *** CD-ROM | disk | FTP | other *** search
- Path: news.rain.org!usenet
- From: "Guus Leeuw jr." <guusl@eiffel.com>
- Newsgroups: comp.lang.c++
- Subject: Re: What is wrong with the code please?
- Date: Sun, 25 Feb 1996 10:47:59 -0800
- Organization: Interactive Software Engineering Inc. http://www.eiffel.com/
- Message-ID: <3130AEDF.3FFCCCBE@eiffel.com>
- References: <4ghbip$a68@nuke.csu.net>
- NNTP-Posting-Host: @outback.eiffel.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
-
- Syrous Etezadi Amoli wrote:
- >
- > Hi
- >
- > Please tell me what is wrong with the following code which implements a
- > linked list in BC++ 4.5 with TListImp?
- >
- > #include <classlib\listimp.h>
- > #include <iostream.h>
- > #include <String.h>
- >
- > void main()
- > {
- >
- > class CIndexListElement : public: string{
- > private:
- > string subject;
- > long start;
- > long end;
- > public:
- >
- > DWORD GetStart(void) {return start;};
- > DWORD GetEnd(void) { return end;};
- > string GetSubject(void) {return subject;};
- > void SetStart(DWORD x) {start = x;};
- > void SetEnd(DWORD x) {end = x;};
- > void SetSubject(string s) { subject = s;};
- > private:
- >
- > };
- > TListImp<CIndexListElement> m_SubjectList;
- >
- > CIndexListElement *new_entry= new CIndexListElement;
- > new_entry->SetSubject("1st subject");
- > new_entry->SetEnd(1);
- > new_entry->SetStart(1);
- > m_SubjectList.Add(new_entry);
-
- I bet this call to `Add' fails to compile, huh?
-
- Let's see... new_entry is a pointer to an object of type CIndexListElement;
- m_SubjectList is an object of type TListImp<CIndexListElement>. Hmm, your
- template is instantiated with type CIndexListElement and you're trying to put
- pointers in it. Either, dereference your pointer, or instantiate your
- template to hold CIndexListElement*.
-
- > new_entry->SetSubject("2ND subject");
- > new_entry->SetEnd(2);
- > new_entry->SetStart(2);
- > m_SubjectList.Add(new_entry);
- > new_entry->SetSubject("3RD subject");
- > new_entry->SetEnd(3);
- > new_entry->SetStart(3);
- > m_SubjectList.Add(new_entry);
- >
-
- When you declare m_SubjectList to be a TListImp<CIndexListElement*>, you
- gotta be darn sure, that the list copies the objects put in it (i.e. does Add
- clone the argument?). If not, you might want to get new objects of type
- CIndexListElement for every Add.
-
- > TListIteratorImp<cIndexListElement> next(m_SubjectList);
- > cout << "\n The stored items are:\n" << next++;
- >
- > while (next)
- > cout << ", " << next++;
- > cout << endl;
-
- Here is one (maybe two) other problem:
- a) you don't delete new_entry;
- (b) your program doesn't return a result. Plus that main is declared to
- return a void type...)
- > }
- >
-
- Hope this helps,
- Guus
-